<template>
  <div class="crowd-activity">
    <c-title :text="`${crowdfund_diy_name}活动`"></c-title>
    <van-tabs v-model="active" @change="changeTap">
      <van-tab :title="`${crowdfund_diy_name}的活动`"></van-tab>
      <van-tab title="自付的活动"></van-tab>
      <van-tab title="支持的活动"></van-tab>
    </van-tabs>
    <ActivityCompon :crowdfund_diy_name="crowdfund_diy_name" :active="active" :listData="listData" v-on:childIndex="getChildIndex"></ActivityCompon>
  </div>
</template>
<script>
import { Toast } from "vant";
import ActivityCompon from "./children/activity";
const selfPaidUrl = "plugin.crowdfunding.frontend.activity-member.selfPaid";
const crowdfundingUrl = "plugin.crowdfunding.frontend.activity-member.crowdfunding";
const supportUrl = "plugin.crowdfunding.frontend.activity-member.support";
export default {
  components: { ActivityCompon },
  name: "CrowdActivity",
  data() {
    return {
      active: 0,
      listData: [],
      activeChild: 0,
      crowdfund_diy_name:'',
      //more
      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    this.getData();
    this.crowdfund_diy_name = JSON.parse(localStorage.getItem("yz_basic_info"))?.plugin_setting?.crowdfund_diy_name;
    this.fun.setWXTitle(this.crowdfund_diy_name+'活动');
  },
  methods: {
    getChildIndex(e) {
      this.init();
      this.activeChild = e;
      this.getData();
    },
    init() {
      this.listData = [];
      this.isLoadMore = true;
      this.page = 1;
      this.total_page = 0;
      this.activeChild = 0;
    },
    changeTap() {
      this.init();
      this.getData();
    },
    getData() {
      let urlstr = "";
      let json = {
        page: this.page
      };
      if (this.active != 1) {
        switch (this.activeChild) {
          case 1:
            json.status = 0;
            break;
          case 2:
            json.status = 1;
            break;
          case 3:
            json.status = 2;
            break;
          default:
            json.status = "";
        }
      }
      if (this.active == 0) {
        urlstr = crowdfundingUrl;
      } else if (this.active == 1) {
        urlstr = selfPaidUrl;
      } else if (this.active == 2) {
        urlstr = supportUrl;
      }
      $http
        .get(urlstr, json, "加载中")
        .then(response => {
          if (response.result === 1) {
            this.isLoadMore = true;
            this.page = 1;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.listData = response.data.data;
          } else {
            Toast(res.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let json = {
          page: this.page
        };
        if (this.active != 1) {
          switch (this.activeChild) {
            case 1:
              json.status = 0;
              break;
            case 2:
              json.status = 1;
              break;
            case 3:
              json.status = 2;
              break;
            default:
              json.status = "";
          }
        }
        let urlstr = "";
        if (this.active == 0) {
          urlstr = crowdfundingUrl;
        } else if (this.active == 1) {
          urlstr = selfPaidUrl;
        } else if (this.active == 2) {
          urlstr = supportUrl;
        }
        $http.post(urlstr, json, "加载中").then(
          response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              var myData = response.data.data;
              this.listData = this.listData.concat(myData); //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              return;
            }
          },
          function (response) {
            // error callback
          }
        );
      }
    }
  }
};
</script>
<style lang="scss" scoped>
// .crowd-activity {}
</style>